// pages/index/index.js
// const WXBizDataCrypt = require('../../WXBizDataCrypt')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  get() {
    wx.getUserProfile({
      desc: '获取用户信息',
      success: res => {
        console.log(res)
        var touxiang = res.userInfo.avatarUrl
        var name = res.userInfo.nickName
        wx.login({
          success: res => {
            console.log(res)
            var code = res.code
            wx.request({
              url: 'https://api.weixin.qq.com/sns/jscode2session?appid=wx1766fdc4329dd7fe&secret=b97f65dfe61604e820ec6dd4916c2972&js_code="041Kb2ll27Orka4Bu8ll2buRPA1Kb2l8"&grant_type=authorization_code', //仅为示例，并非真实的接口地址
              header: {
                'content-type': 'application/json' // 默认值
              },
              success(res) {
                console.log(res.data)
              }
            })
            wx.getUserInfo({
              success: res => {
                console.log(res)
                var encryptedData = res.encryptedData
                var iv = res.iv
              }
            })
          }
        })
      }
    })

  },


  getUserProfile() {
    const _this = this
    //无法调用时使用老版本替代
    wx.getSetting({
      success: function (res) {
        if (!res.authSetting['scope.userInfo']) {
          //进入未授权 -- wx.getUserInfo获取用户信息 
          wx.getUserInfo({
            success: function (ress) {
              //ress用户数据
              console.log(ress)
            }
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function() {
    var that = this;
    // 查看是否授权
    wx.getSetting({
      success (res){
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称
          wx.getUserInfo({
            success: function(res) {
              console.log(res.userInfo)
              that.setData({
                result:'ok',// 结果
                nickName:res.userInfo.nickName,// 微信昵称
                avatarUrl:res.userInfo.avatarUrl,// 微信头像
              })
            }
          })
        }else{
          // 未授权，结果返回null
          that.setData({
            result:'null',// 结果
          })
        }
      }
    })
  },
  // 请求API授权，获得用户头像和昵称
  bindGetUserInfo (e) {
    console.log(e.detail.userInfo.nickName)
    var that = this;
    that.setData({
      result:'ok',// 结果
      nickName:e.detail.userInfo.nickName,// 微信昵称
      avatarUrl:e.detail.userInfo.avatarUrl,// 微信头像
    })
  }

})